<?php
// no direct access
defined( '_AppMiage' ) or die( 'Restricted access' );
if(isEtudiant()){
    //include header
    include_once (DS.'include/header.php');
    //include menu
    include_once (DS.'include/menu.php');

    $showOK = false;
    $showError = false;

    if(isset($_POST['submit']) && !empty($_POST['submit']))
    {

        $form = true;
        $errMSG = array();
        $check['offre'] = false;
        $check['entreprise'] = false;
        $check['user'] = false;
        $check['stage'] = false;
        $check['contact'] = false;

        // MAJ DES INFOS PERSOS
        if(isset($_POST['PERSO']['name']) && !empty($_POST['PERSO']['name']))
        {
            $tmp = $global_member->setNomUtilisateur($_POST['PERSO']['name']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['prenom']) && !empty($_POST['PERSO']['prenom']))
        {
            $tmp = $global_member->setPrenomUtilisateur($_POST['PERSO']['prenom']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['prenom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['mail']) && !empty($_POST['PERSO']['mail']))
        {
            $tmp = $global_member->setEmailUtilisateur($_POST['PERSO']['mail']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['add1']) && !empty($_POST['PERSO']['add1']))
        {
            $tmp = $global_member->setAdresse1($_POST['PERSO']['add1']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['add1'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['add2']) && !empty($_POST['PERSO']['add2']))
        {
            $tmp = $global_member->setAdresse2($_POST['PERSO']['add2']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['add2'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['PERSO']['ville']) && !empty($_POST['PERSO']['ville']))
        {
            $tmp = $global_member->setVille($_POST['PERSO']['ville']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['ville'] = $tmp['errmsg'];
                $form = false;
            }
        }


        if(isset($_POST['PERSO']['cp']) && !empty($_POST['PERSO']['cp']))
        {
            $tmp = $global_member->setCP($_POST['PERSO']['cp']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['PERSO']['cp'] = $tmp['errmsg'];
                $form = false;
            }
        }
        // FIN MAJ INFOS PERSO
        //
        // GESTION ENTREPRISE
        if(isset($_POST['ENT']['IdEntreprise']) && !empty($_POST['ENT']['IdEntreprise']))
        {
            // ENTREPRISE EXISTANT
            $IdEntreprise = $_POST['ENT']['IdEntreprise'];
            $check['entreprise'] = true;
        }
        else
        {
            // ENTREPRISE INEXISTANTE
            $entreprise = new Entreprise();
            if(isset($_POST['ENT']['Nom']) && !empty($_POST['ENT']['Nom']))
            {
                $tmp = $entreprise->setNomEntreprise($_POST['ENT']['Nom']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['nom'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['phone']) && !empty($_POST['ENT']['phone']))
            {
                $tmp = $entreprise->setTelephoneEntreprise($_POST['ENT']['phone']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['phone'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['url']) && !empty($_POST['ENT']['url']))
            {
                $tmp = $entreprise->setSiteEntreprise($_POST['ENT']['url']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['url'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['add1']) && !empty($_POST['ENT']['add1']))
            {
                $tmp = $entreprise->setAdresse1Entreprise($_POST['ENT']['add1']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['add1'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['add2']) && !empty($_POST['ENT']['add2']))
            {
                $tmp = $entreprise->setAdresse2Entreprise($_POST['ENT']['add2']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['add2'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['ville']) && !empty($_POST['ENT']['ville']))
            {
                $tmp = $entreprise->setVilleEntreprise($_POST['ENT']['ville']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['ville'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ENT']['cp']) && !empty($_POST['ENT']['cp']))
            {
                $tmp = $entreprise->setCPEntreprise($_POST['ENT']['cp']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ENT']['cp'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            $entreprise->insertEntreprise();
            if($entreprise->requeteOk === true)
            {
                $check['entreprise'] = true;
                $IdEntreprise = $entreprise->getIdEntreprise();
            }
        }
        // FIN GESTION ENTREPRISE

        if($form === true)
        {
            $global_member->update();
            if($global_member->requeteOk === true )
            {
                $check['user'] = true;
            }


            if($check['entreprise'] === true && !empty($IdEntreprise))
            {
                // INSERT TUTEUR
                if(isset($_POST['TUTEUR']['mail']) && !empty($_POST['TUTEUR']['mail']))
                {
                    $contact = ContactEntreprise::getUserByLogin($_POST['TUTEUR']['mail']);
                    
                    if(isset($contact) && !empty($contact))
                    {
                        // TUTEUR EXISTE DEJA
                        $IdTuteur = $contact->IdUtilisateur;
                        $check['contact'] = true;
                    }
                    else
                    {
                        // ON CREE LE TUTEUR EN TANT QUE CONTACT ENTREPRISE
                        // LINKE AVEC L'ENTREPRISE CREEE EN MEME TEMPS
                        $contact = new ContactEntreprise();
                         $form2= true;
                        if(isset($_POST['TUTEUR']['nom']) && !empty($_POST['TUTEUR']['nom']))
                        {
                            $tmp = $contact->setNomUtilisateur($_POST['TUTEUR']['nom']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['nom'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['prenom']) && !empty($_POST['TUTEUR']['prenom']))
                        {
                            $tmp = $contact->setPrenomUtilisateur($_POST['TUTEUR']['prenom']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['prenom'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['mail']) && !empty($_POST['TUTEUR']['mail']))
                        {
                            $tmp = $contact->setEmailUtilisateur($_POST['TUTEUR']['mail']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['mail'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['fonction']) && !empty($_POST['TUTEUR']['fonction']))
                        {
                            $tmp = $contact->setFonction($_POST['TUTEUR']['fonction']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['fonction'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }

                        if(isset($_POST['TUTEUR']['tel']) && !empty($_POST['TUTEUR']['tel']))
                        {
                            $tmp = $contact->setTelephoneContactEntreprise($_POST['TUTEUR']['tel']);
                            if($tmp['boolOk'] === false)
                            {
                                $errMSG['TUTEUR']['tel'] = $tmp['errmsg'];
                                $form2 = false;
                            }
                        }
                        $contact->setIdEntreprise((int)$IdEntreprise);
                        $contact->setIdTypeUser("4");
                        $contact->setDateCreation();
                        if($form2)
                        {
                            $contact->insert();
                            var_dump($contact);

                            if($contact->requeteOk === true)
                            {
                                $IdTuteur = $contact->getIdUtilisateur();
                                $check['contact'] = true;
                            }
                        }

                    }
                }
            }


            if($check['user'] === true && $check['entreprise'] === true && $check['contact'] === true)
            {
                $form4 = true;
                $IdOffre = $_POST['idOffrePortail'];
                $stage = new Stage();
                $stage->setIdEtudiant($global_member->getIdUtilisateur());
                $stage->setEtatFiche("en attente");
                $stage->setIdTuteurEntreprise($IdTuteur);
                $stage->setIdOffre($IdOffre);
                $stage->setIdPromo($global_member->getIdPromo());
                $stage->setDateCreationFicheStage();
                if(isset($_POST['IG']['NomStage']) && !empty($_POST['IG']['NomStage']))
                {
                    $tmp = $stage->setTitreStage($_POST['IG']['NomStage']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['NomStage'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if(isset($_POST['IG']['Description']) && !empty($_POST['IG']['Description']))
                {
                    $tmp = $stage->setDescriptionStage($_POST['IG']['Description']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['Description'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }

                if(isset($_POST['IG']['dDebut']) && !empty($_POST['IG']['dDebut']))
                {
                    $tmp = $stage->setDateDebutStage(Tools::transformDateToTimestamp($_POST['IG']['dDebut']));
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['dDebut'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if(isset($_POST['IG']['dFin']) && !empty($_POST['IG']['dFin']))
                {
                    $tmp = $stage->setDateFinStage(Tools::transformDateToTimestamp($_POST['IG']['dFin']));
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['IG']['dFin'] = $tmp['errmsg'];
                        $form4 = false;
                    }
                }
                if($form4)
                {
                    $stage->insertStage();
                    if($stage->requeteOk === true)
                    {
                        $IdStage = $stage->getIdStage();

                        $of = new Offre($_POST['idOffrePortail']);
                        $of->enleveUnPoste();
                        $of->updateOffre();

                        $check['stage'] = true;
                        $showOK = true;
                        ?>
                        <script type="text/javascript">
                            window.location.href="<?php echo URL; ?>index.php?m=stage_etudiant&ok=1";
                        </script>
                        <?php
                    }
                }
            }
        }
        if($check['user'] === false || $check['entreprise'] === false
                || $check['contact'] === false || $check['offre'] === false
                || $check['stage'] === false)
        {
            $showError = true;
        }
    }

    $lstEntreprise = Entreprise::getList();
    $lstDomaine = DomaineApplication::getListDomaineApplication();
    $lstTechno = Technologie::getList();
    $lstCategorie = Categorie::getListCategory();
    ?>
    <?php if(!isset($_POST['oid']) && empty($_POST['oid'])){ ?>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#form_offre").hide();
    });
    function loadOffre()
    {
        ajax.getFromByIdOffre($('#id_offre').val());
    }
    </script>
    <?php }else{ ?>
    <script type="text/javascript">
    $(document).ready(function(){
        ajax.getFromByIdOffre(<?php if(isset($_POST['oid']) && !empty($_POST['oid'])){echo $_POST['oid']; } ?>);
    });
    </script>
    <?php } ?>
    <div id="content">
        <div class="post">
            <div class="entry">
                <h1 class="title">Cr&eacute;ation d'une fiche de stage</h1>
                &nbsp;
            </div>
            <div class="entry" id="specify_id">
                <label>Veuillez sp&eacute;cifier l'ID de l'offre : </label>
                <input type="text" id="id_offre" />
                <a href="#" onclick="loadOffre()" class="button green">Valider</a>
            </div>
            <div id="tg"></div>
            <div class="entry" id="form_offre">
                <form method="post" action="<?php echo URL; ?>index.php?m=submit_offre_portail" id="custom">
                    <input type="hidden" name="idOffrePortail" id="idOffrePortail" value="<?php if(isset($_POST['oid']) && !empty($_POST['oid'])){echo $_POST['oid']; } ?>"/>
                    <fieldset title="&Eacute;tape 1">
                        <legend>Informations G&eacute;n&eacute;rales</legend>
                        <label for="IG[NomStage]">Titre du stage : </label><br />
                        <input type="text" class="text" name="IG[NomStage]" value="<?php echo $_POST['IG']['NomStage']; ?>"/>
                        <br />
                        <label for="IG[dDebut]">Date de debut : </label><br />
                        <input type="text" class="text datepicker" name="IG[dDebut]" value="<?php echo $_POST['IG']['dDebut']; ?>"/>
                        <br />
                        <label for="IG[dFin]">Date de fin : </label><br />
                        <input type="text" class="text datepicker" name="IG[dFin]" value="<?php echo $_POST['IG']['dFin']; ?>"/>
                        <br />
                        <label for="IG[Description]">Description : </label>
                        <span class="miniInfos">*Le sujet doit &ecirc;tre en ad&eacute;quation avec le niveau du dipl&ocirc;me MIAGE pr&eacute;par&eacute;</span><br />
                        <textarea name="IG[Description]" class="text"><?php echo $_POST['IG']['Description']; ?></textarea>
                        <br />
                    </fieldset>
                    <fieldset title="&Eacute;tape 2">
                        <legend>Entreprise</legend>
                        <div id="OldEntreprise">
                            <input type="hidden" name="ENT[IdEntreprise]" value="0" id="IdEntreprise" />
                            <label for="NomEntreprise">Nom : </label>
                            <span id="nom"></span>
                            <br />
                            <label for="add1Entreprise">Adresse : </label>
                            <span id="add1"></span>
                            <br />
                            <label for="add2Entreprise">Adresse compl&eacute;mentaire : </label>
                            <span id="add2"></span>
                            <br />
                            <label for="cpEntreprise">Code Postal : </label>
                            <span id="cp"></span>
                            <br />
                            <label for="villeEntreprise">Ville : </label>
                            <span id="ville"></span>
                            <br />
                            <label for="paysEntreprise">Pays : </label>
                            <span id="pays"></span>
                            <br />
                            <label for="phoneEntreprise">T&eacute;l&eacute;phone : </label>
                            <span id="tel"></span>
                            <br />
                            <label for="urlEntreprise">Site web : </label>
                            <span id="url"></span>
                            <br />
                        </div>
                    </fieldset>
                    <fieldset title="&Eacute;tape 3">
                        <legend>Tuteur</legend>
                        <label for="TUTEUR[nom]">Nom : </label><br />
                        <input type="text" class="text " name="TUTEUR[nom]"/>
                        <br />
                        <label for="TUTEUR[prenom]">Pr&eacute;nom : </label><br />
                        <input type="text" class="text " name="TUTEUR[prenom]"/>
                        <br />
                        <label for="TUTEUR[fonction]">Fonction : </label><br />
                        <input type="text" class="text " name="TUTEUR[fonction]"/>
                        <br />
                        <label for="TUTEUR[tel]">T&eacute;l&eacute;phone : </label><br />
                        <input type="text" class="text " name="TUTEUR[tel]"/>
                        <br />
                        <label for="TUTEUR[mail]">Email : </label><br />
                        <input type="text" class="text  email" name="TUTEUR[mail]"/>
                        <br />
                    </fieldset>
                    <fieldset title="&Eacute;tape 4">
                        <legend>Mes Informations</legend>
                        <label for="PERSO[name]">Nom : </label><br />
                        <input class="text " type="text" name="PERSO[name]" id="name" value="<?php echo $global_member->getNomUtilisateur(); ?>"/><br />
                        <br />
                        <label for="PERSO[prenom]">Pr&eacute;nom : </label><br />
                        <input class="text " type="text" name="PERSO[prenom]" id="prenom" value="<?php echo $global_member->getPrenomUtilisateur(); ?>"/><br />
                        <br />
                        <label for="PERSO[mail]">Email : </label><br />
                        <input class="text  email" type="text" name="PERSO[mail]" id="mail" value="<?php echo $global_member->getEmailUtilisateur(); ?>"/><br />
                        <br />
                        <label for="PERSO[add1]">Adresse : </label><br />
                        <input class="text " type="text" name="PERSO[add1]" id="add1" value="<?php echo $global_member->getAdresse1(); ?>"/><br />
                        <br />
                        <label for="PERSO[add2]">Adresse compl&eacute;mentaire : </label><br />
                        <input class="text" type="text" name="PERSO[add2]" id="add2" value="<?php echo $global_member->getAdresse2(); ?>"/><br />
                        <br />
                        <label for="PERSO[cp]">Code Postal : </label><br />
                        <input class="text " type="text" name="PERSO[cp]" id="cp" value="<?php echo $global_member->getCP(); ?>"/><br />
                        <br />
                        <label for="PERSO[ville]">Ville : </label><br />
                        <input class="text " type="text" name="PERSO[ville]" id="ville" value="<?php echo $global_member->getVille(); ?>"/><br />
                        <br />
                        <!--label for="PERSO[tel]">T&eacute;l&eacute;phone : </label><br />
                        <input type="text" class="text" name="PERSO[tel]"/>
                        <br /-->
                    </fieldset>
                    <input type="submit" class="finish" disable="disable" value="Cr&eacute;er" name="submit">
                </form>
            </div>
        </div>
    </div>
    <?php
    include_once (DS.'include/sidebar.php');
    include_once (DS.'include/footer.php');
}
else
{
    redirectTo();
}
?>